package gui;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import util.Protocol;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import net.BackClient;
import net.ReceiveInformation;

public class FriendList extends JFrame {

	private JPanel contentPane;

	String nickname;
	
	String friendnickname ;
	
	JLabel []list = new JLabel[10];
	
	String[] friendID;
	
	ReceiveInformation information = new ReceiveInformation();

	Thread thread = new Thread(new ReceiveInformation());
	
	int count; 
	
	Protocol protocol = new Protocol();
	
	BackClient backClient = new BackClient();
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FriendList frame = new FriendList();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public FriendList() {
		information.addthisWindows("friendlist", this);	
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 228, 433);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		final JLabel lblNewLabel = new JLabel(" 好友列表");
		lblNewLabel.setIcon(new ImageIcon("3.png"));
		lblNewLabel.setBounds(10, 10, 192, 15);
		contentPane.add(lblNewLabel);
		JButton button = new JButton("添加好友");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				AddFriendUI addFriendUI = new AddFriendUI();
				addFriendUI.getNickname(nickname);
				addFriendUI.setVisible(true);
			}
		});
		button.setBounds(10, 359, 95, 25);
		contentPane.add(button);
		thread.start();
		
		lblNewLabel.addMouseListener(new MouseAdapter() 
		{
			int isdrag = 1;
			@Override
			public void mouseClicked(MouseEvent e) 
			{
				if(e.getClickCount()==1 )
				{
					if(isdrag%2==0)
					{
						lblNewLabel.setIcon(new ImageIcon("3.png"));
						for(int i=0;i<friendID.length;i++)
						{
							list[i].setVisible(true);
						}
					}
					if(isdrag%2==1)
					{
						lblNewLabel.setIcon(new ImageIcon("aa.png"));
						for(int i=0;i<friendID.length;i++)
						{
							list[i].setVisible(false);
						}
					}
					isdrag++;
				}
			}
		});
	}
	
	public void setNickname(String nickname)
	{
		this.nickname = nickname;
		String offlinemessage = protocol.getOfflineMassage(nickname);	
		backClient.SendMassage(offlinemessage);
		String okmassage = protocol.backOfflineMassageOk(nickname);
		backClient.SendMassage(okmassage);
	}
	
	public void setFirendID(String friendnickname)
	{
		this.friendnickname = friendnickname;
		friendID = friendnickname.split(",");
		
		final String[] id = new String[50];
		
		for(int i=0;i<friendID.length;i++)
		{
			list[i] = new JLabel(friendID[i]);
			
			final String aid = friendID[i];
			list[i].setBounds(20, 30*(i+1), 182, 22);
			contentPane.add(list[i]);
			list[i].addMouseListener(new MouseAdapter() 
			{
				@Override
				public void mouseClicked(MouseEvent e) 
				{
					if(e.getClickCount()==2)
					{
						UserUI ui = new UserUI();
						ui.getfrinedID(aid);
						ui.setNickname(nickname);
						ui.setVisible(true);
					}
				}
			});
		}
		count = friendID.length;
	}
	
	public void setNewFriend(String friendID)
	{	
		list[count] = new JLabel(friendID);
		list[count].setBounds(20, 30*(count+1), 182, 22);
		contentPane.add(list[count]);
		
		final String aid = friendID;
		list[count].addMouseListener(new MouseAdapter() 
		{
			@Override
			public void mouseClicked(MouseEvent e) 
			{
				if(e.getClickCount()==2)
				{
					UserUI ui = new UserUI();
					ui.getfrinedID(aid);
					ui.setNickname(nickname);
					ui.setVisible(true);
				}
			}
		});
		contentPane.repaint();
	}
}

